package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.PublishResultDetailRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.PublishResultDetail;

import java.util.List;

/**
 * Created by hunk.qin on 2015/3/12.
 */
@Service
public class PublishResultDetailRdbServiceImpl extends DaoTemplate implements PublishResultDetailRdbService {

    @Override
    public long countDetailByPublishResultIdAndTypes(Long publishResultId, String types) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT COUNT(1) FROM publish_result pr " +
                "INNER JOIN publish_result_record prr ON prr.publish_result_id = pr.publish_result_id " +
                "INNER JOIN publish_result_detail prd ON prd.publish_result_record_id = prr.publish_result_record_id WHERE 1=1 ");

        sql.append(" AND pr.publish_result_id =" + publishResultId);
        sql.append(" AND prr.type IN (" + types + ")");
        long count = getJdbcTemplate().queryForLong(sql.toString());
        return count;
    }

    @Override
    public List<PublishResultDetail> searchDetailByPublishResultIdAndTypes(Long publishResultId, String types) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT prd.publish_result_detail_id,prd.publish_result_record_id,prd.result,prd.objectType,prd.param,prd.exception,prd.type FROM publish_result pr " +
                "INNER JOIN publish_result_record prr ON prr.publish_result_id = pr.publish_result_id " +
                "INNER JOIN publish_result_detail prd ON prd.publish_result_record_id = prr.publish_result_record_id WHERE 1=1 ");

        sql.append(" AND pr.publish_result_id =" + publishResultId);
        sql.append(" AND prr.type IN (" + types + ")");

        List list = (List) getJdbcTemplate().query(sql.toString(), DefaultRowMapper.autoRowMapper(PublishResultDetail.class));
        if (list != null && list.size() > 0) {
            return list;
        } else {
            return null;
        }
    }

    @Override
    public Long save(PublishResultDetail publishResultDetail) {
        return saveResultId(publishResultDetail);
    }
}
